<template>
	<view>
		<u-gap height="10" bgColor="#ffffff"></u-gap>
		<view class="lable">
			<u-avatar class="mag_left_little" size="80" :src="avatarUrl" shape="circle" @click="upAvatar"></u-avatar>
		</view>
		<view style="text-align: center;font-size: 25px;">
			<u--text :text="username=='暂未登录'?'':username" align='center' size='25px' @click='toUpUserInfo'></u--text>
		</view>
		<u-loading-icon :show='loading' size="36" text='登录中' :vertical='true'></u-loading-icon>
		<u-gap height="10" bgColor="#ffffff"></u-gap>
		<u-gap height="10" bgColor="#f8f8f8"></u-gap>
		<u-cell @click="toWallet" icon="../../static/icon/moneybar.png" titleStyle="margin-left:4px" size="large"
			title="钱包" :isLink="true" arrow-direction="left"></u-cell>
		<u-gap height="10" bgColor="#f8f8f8"></u-gap>
		<view>
			<u-cell @click="toUpGoods" icon="../../static/icon/upgood.png" titleStyle="margin-left:4px" size="large"
				title="上架" :isLink="true" arrow-direction="left"></u-cell>
			<u-cell @click="toOrderList" icon="../../static/icon/orderinfo.png" titleStyle="margin-left:4px"
				size="large" title="订单" :isLink="true" arrow-direction="left"></u-cell>

			<u-cell @click="toAboutUs" icon="../../static/icon/aboutus.png" titleStyle="margin-left:4px" size="large"
				title="关于我们" :isLink="true" arrow-direction="left"></u-cell>
			<u-cell @click="toSetting" icon="setting" titleStyle="margin-left:4px" size="large" title="设置"
				:isLink="true" arrow-direction="left"></u-cell>
		</view>
	</view>
</template>

<script>
	import {
		updateAvatar
	} from '@/config/api.js'
	export default {
		data() {
			return {
				avatarUrl: '',
				username: '暂未登录',
				loading: true
			}
		},
		onLoad() {
			//如果用户没有登录，需要登录跳转，登录了就执行用户信息读取
			let is_login = '';
			uni.getStorage({
				key: 'is_login',
				success: function(res) {
					is_login = res.data
				}
			})

			console.log(is_login);
			setTimeout(() => {
				if (is_login == 'yes') {
					let name = null;
					let token = null;
					uni.getStorage({
						key: 'user_name',
						success: function(res) {
							name = res.data
						}
					});
					uni.getStorage({
						key: 'user_token',
						success: function(res) {
							token = res.data
						}
					});
					setTimeout(() => {
						this.loading = !this.loading;
						uni.$u.http.get(`/serviceUser/user/info/${name}`, {
							params: {
								token: token
							}
						}).then((res) => {
							console.log("头像地址" + res.info.userAvatar);
							this.avatarUrl = res.info.userAvatar;
							this.username = res.info.userNickname
						}).catch((error) => {
							console.log(error)
						})

					}, 300)

				} else {
					uni.navigateTo({
						url: '../login/login'
					})
				}
			}, 500)


		},
		onShow() {
			//如果用户没有登录，需要登录跳转，登录了就执行用户信息读取
			let is_login = '';
			uni.getStorage({
				key: 'is_login',
				success: function(res) {
					is_login = res.data
				}
			})

			console.log(is_login);
			setTimeout(() => {
				if (is_login == 'yes') {
					let name = null;
					let token = null;


					uni.getStorage({
						key: 'user_name',
						success: function(res) {
							name = res.data
						}
					});
					uni.getStorage({
						key: 'user_token',
						success: function(res) {
							token = res.data
						}
					});
					setTimeout(() => {
						this.loading = false;
						uni.$u.http.get(`/serviceUser/user/info/${name}`, {
							params: {
								token: token
							}
						}).then((res) => {
							console.log("头像地址" + res.info.userAvatar);
							this.avatarUrl = res.info.userAvatar;
							this.username = res.info.userNickname
						}).catch((error) => {
							console.log(error)
						})

					}, 1000)

				} else {
					uni.navigateTo({
						url: '../login/login'
					})
				}
			}, 1000)


		},
		methods: {
			upAvatar() {
				//上传头像  todo
				uni.$u.toast("进入上传头像的方法")
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						//选择好之后将图片上传到阿里云oos返回url之后再发送请求给后端修改用户头像
						const tempFilePaths = res.tempFilePaths;
						uni.uploadFile({
							url: 'http://localhost:8080/upload/fileoss/upAvatar', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							formData: {
								'user': 'test'
							},
							success: (uploadFileRes) => {
								let info = JSON.parse(uploadFileRes.data)
								let username = '';
								uni.getStorage({
									key: 'user_name',
									success: function(res) {
										username = res.data;
									}
								})
								// console.log(username + '----186----' + info.data.url);
								updateAvatar({
									name: username,
									url: info.data.url
								}).then(res => {
									console.log("进入修改头像的方法");
									uni.$u.toast("修改成功")
								}).catch((error) => {
									console.log(error);
								})
							}
						});
					}
				});
			},
			toUpGoods() {
				uni.navigateTo({
					url: 'upgood/upgood'
				})
			},
			toSetting() {
				uni.navigateTo({
					url: 'setting/setting'
				})
			},
			toAboutUs() {
				uni.navigateTo({
					url: 'aboutUs/aboutUs'
				})
			},
			toOrderList() {
				uni.navigateTo({
					url: 'orderList/orderList'
				})
			},
			toWallet() {
				uni.navigateTo({
					url: 'Wallet/Wallet'
				})
			},
			toUpUserInfo() {
				uni.navigateTo({
					url: 'userInfo/userInfo'
				})
			}
		}
	}
</script>

<style lang="scss">
	.mag_left_little {
		margin-left: 20rpx;
	}

	.font-size_mid {
		font-size: 80rpx;
	}

	.mag_left_big {
		margin-left: 40rpx;
	}

	.lable {
		display: flex;
		justify-content: center;
	}
</style>
